﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Dao;

namespace Web.Page
{
    public partial class Export : System.Web.UI.Page
    {
        protected KC_WLPD_Dao dao = new KC_WLPD_Dao();

        string bh;
        string mc;
        string ckid;
        string cwid;

        protected void Page_Load(object sender, EventArgs e)
        {
            bh = Request.QueryString["bh"];
            mc = Request.QueryString["mc"];
            ckid = Request.QueryString["ckid"];
            cwid = Request.QueryString["cwid"];

            string temp = string.Format("attachment;filename={0}", "erp.csv");
            // Response.Clear();
            Response.ContentType = "application/ms-excel"; // "text/csv";//application/ms-excel
            Response.AddHeader("Content-Disposition", temp);
            Response.ContentEncoding = Encoding.Default;
            Response.Write(exportCsv());
            Response.Flush();
            Response.End();
        }

        protected List<vWLKC> query()
        {
            return dao.QueryBySql(bh, mc, ckid, cwid);
        }

        protected string exportCsv()
        {
            List<vWLKC> list = query();
            if (list != null)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("编号");
                sb.Append(",名称");
                sb.Append(",类型");
                sb.Append(",数量");
                sb.Append(",安全数量");
                sb.Append(",单位");
                sb.Append(",仓库");
                sb.Append(",仓位");
                sb.Append(",供应商");
                sb.Append("\r\n");

                foreach (vWLKC item in list)
                {
                    sb.Append(item.BH);
                    sb.Append(",");
                    sb.Append(item.MC);
                    sb.Append(",");
                    sb.Append(item.LXMC);
                    sb.Append(",");
                    sb.Append(item.SL);
                    sb.Append(",");
                    sb.Append(item.AQKCSL);
                    sb.Append(",");
                    sb.Append(item.DWMC);
                    sb.Append(",");
                    sb.Append(item.CKMC);
                    sb.Append(",");
                    sb.Append(item.CWBH);
                    sb.Append(",");
                    sb.Append(item.GYSMC);
                    sb.Append("\r\n");
                }

                return sb.ToString();
            }

            return null;
        }
    }
}